<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-1717133-7']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<?php
require_once '../../../wp-blog-header.php';

$bounces = buzzr_download_page('https://sendgrid.com/api/bounces.get.json?api_user=kunal.jain@enthrallmedia.com&api_key=enthrall&date=1');
//$bounces = json_decode($bounces);
$emails['bounces'] = json_decode($bounces);

$invalid_emails = buzzr_download_page('https://sendgrid.com/api/invalidemails.get.json?api_user=kunal.jain@enthrallmedia.com&api_key=enthrall&date=1');
//$invalid_emails = json_decode($invalid_emails);
$emails['invalid_emails'] = json_decode($invalid_emails);

$spam_reports = buzzr_download_page('https://sendgrid.com/api/spamreports.get.json?api_user=kunal.jain@enthrallmedia.com&api_key=enthrall&date=1');      
//$spam_reports = json_decode($spam_reports);
$emails['spam_reports'] = json_decode($spam_reports);

//$merged_array = array_merge($bounces, $invalid_emails, $spam_reports);

//printr($emails,1);

$newsletter_pod = new Pod('newsletter');
$api = new PodAPI();


foreach($emails as $type => $email_ids){
	if(count($email_ids) > 0){
  	
	foreach($email_ids as $key => $email_id){
  	  $slug = sanitize_title($email_id->email);
  	  $newsletter_pod->findRecords('id DESC', 1, "t.slug = '$slug'");
	  while ($newsletter_pod->fetchRecord()){
	    $params = array('tbl_row_id' => $newsletter_pod->get_field('id'),'datatype'=>'newsletter');
	    $api->drop_pod_item($params);
	  }
	  
	switch ($type){
	  	case "bounces":
	  		buzzr_download_page('https://sendgrid.com/api/bounces.delete.json?api_user=kunal.jain@enthrallmedia.com&api_key=enthrall&email='.$email_id->email);
?>
<script type="text/javascript">
	_gaq.push(['_trackEvent', 'Cleaned Up Emails', 'Bounces', '<?php echo $email_id->email; ?>']);
</script>
<?php	  	
	  	break;
	  	case "invalid_emails":
	  		buzzr_download_page('https://sendgrid.com/api/invalidemails.delete.json?api_user=kunal.jain@enthrallmedia.com&api_key=enthrall&email='.$email_id->email);
?>
<script type="text/javascript">
	_gaq.push(['_trackEvent', 'Cleaned Up Emails', 'Invalid Emails', '<?php echo $email_id->email; ?>']);
</script>
<?php	  	
	  	break;
	  	case "spam_reports":
  			buzzr_download_page('https://sendgrid.com/api/spamreports.delete.json?api_user=kunal.jain@enthrallmedia.com&api_key=enthrall&email='.$email_id->email);
?>
<script type="text/javascript">
	_gaq.push(['_trackEvent', 'Cleaned Up Emails', 'Spam', '<?php echo $email_id->email; ?>']);
</script>
<?php
	  	break;
	  }
	  
	}
  }
  
 }
?>